$(function () {
    //常用jQuery对象
    var entity_datagrid = $("#entity_list");
    var entity_dailog = $("#entity_dialog");
    var entity_form = $("#entity_form");
    var allPermission = $("#allPermission");
    var hasPersmission = $("#hasPersmission");

    //==========================================================================
    //封装方法
    var methodObj = {
        //添加按钮方法
        add: function () {
            entity_dailog.dialog("open");
            entity_dailog.dialog("setTitle", "新增");
            allPermission.datagrid("load");
        },

        //编辑按钮方法

        //编辑按钮方法
        edit: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            entity_dailog.dialog("open");
            entity_dailog.dialog("setTitle", "编辑");
            entity_dailog.form("load", row);
            hasPersmission.datagrid("load", {roleId: row.id});
        },


        //编辑或新增方法具体逻辑
        saveOrUpdate: function () {
            entity_form.form("submit", {
                url: "/role/saveOrUpdate.do",
                onSubmit: function (param) {
                    var rows =  hasPersmission.datagrid("getRows");
                    for(var i=0;i<rows.length;i++){
                        param["permissions["+i+"].id"]=rows[i].id;
                    }
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            entity_dailog.dialog("close");
                            entity_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            entity_dailog.dialog("close");
                        });
                    }
                }
            })
        },


        //删除角色
        btn_delete: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            $.messager.confirm('温馨提示',"请问确定要删除此角色吗?", function (r) {
                if (r) {
                    $.get("/role/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                entity_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }, "json")
                }
            });

        },

        //刷新页面
        reloadList: function () {
            entity_datagrid.datagrid("reload");
        }

    }


    //==========================================================================================
    //按钮绑定事件
    $("[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

    //页面数据加载
    entity_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        url: "/role/query.do",
        singleSelect: "true",
        pagination: true,
        rownumbers: true,
        columns: [[
            {field: 'sn', title: '角色编码', width: 100, align: "center"},
            {field: 'name', title: '角色名称', width: 100, align: "center"}
        ]],
        toolbar: "#tools"
    });

    //编辑添加弹出框设置
    entity_dailog.dialog({
        width: 550,
        height: 450,
        closed: true,
        onClose: function () {
            entity_form.form("clear");
            hasPersmission.datagrid("loadData",[]);
            allPermission.datagrid("load");
        },
        buttons: [{
            text: '保存',
            iconCls: "icon-save",
            handler: methodObj["saveOrUpdate"]
        }, {
            iconCls: "icon-cancel",
            text: '取消',
            handler: function () {
                entity_dailog.dialog("close");
            }
        }]
    });
    //编辑添加弹出框--datagriad -- allpermission
    allPermission.datagrid({
        url: "/permission/selectAll.do",
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'id', hidden: true},
            {field: 'name', title: '权限表达式', width: 100, align: "center"},
        ]],
        onClickRow: function (index, row) {
            hasPersmission.datagrid("appendRow", row);
            allPermission.datagrid("deleteRow", index);
        }
    });
    //编辑添加弹出框--datagriad--haspermission
    hasPersmission.datagrid({
        fit: true,
        url: "/permission/getPermissionsByRoleId.do",
        fitColumns: true,
        columns: [[
            {field: 'id', hidden: true},
            {field: 'name', title: '权限表达式', width: 100, align: "center"},
        ]],
        onClickRow: function (index, row) {
            allPermission.datagrid("appendRow", row);
            hasPersmission.datagrid("deleteRow", index);
        },
        onLoadSuccess: function (data) {
            var allrows = allPermission.datagrid("getRows");
            var rowIds = $.map(data.rows, function (ele) {
                return ele.id;
            });
            for (var i = allrows.length-1; i >= 0; i--) {
                if ($.inArray(allrows[i].id, rowIds) >= 0) {
                    allPermission.datagrid("deleteRow", i);
                }
            }
        },

    });

    //form表单设置
    entity_dailog.form({
        fit: true
    });

});

